﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Text;
using System.Collections.ObjectModel;
using System.ComponentModel;

namespace SilkenWeb.Reg
{
    /// <summary>
    /// Object to represent a list of Candidate objects.
    /// </summary>
    /// <remarks>
    /// Author: Keith Jackson
    /// Date: July 2008
    /// Copyright 2008 S-Cool Ltd.
    /// </remarks>
    public class CandidateCollection : Collection<Candidate>
    {

        #region | Properties & Fields |

        /// <summary>
        /// Gets a list of Candidates that have not yet been rejected.
        /// </summary>
        public ReadOnlyCandidateCollection ActiveCandidates
        {
            get { return GetList(false); }
        }

        /// <summary>
        /// Gets a list of Candidates that have been rejected.
        /// </summary>
        public ReadOnlyCandidateCollection RejectedCandidates
        {
            get { return GetList(true); }
        }

        #endregion

        #region | Public Methods |

        #region | Add (Overloads) |

        /// <summary>
        /// Creates a new REG Candidate.
        /// </summary>
        /// <param name="candidateName">The name of the candidate to create.</param>
        public void Add(string candidateName)
        {
            this.Add(new Candidate(candidateName));
        }

        /// <summary>
        /// Creates a new REG Candidate.
        /// </summary>
        /// <param name="candidate">The candidate to create.</param>
        public new void Add(Candidate candidate)
        {
            if (!this.Contains(candidate.Name))
            {
                base.Add(candidate);
            }
        }

        #endregion

        /// <summary>
        /// Gets a flag to indicate if a given candidate is in the list.
        /// </summary>
        /// <param name="candidateName">The name of the candidate.</param>
        /// <returns>A flag.</returns>
        public bool Contains(string candidateName)
        {
            foreach (Candidate c in this)
            {
                if (c.Name == candidateName)
                {
                    return true;
                }
            }

            return false;
        }

        #endregion

        #region | Private Methods |

        /// <summary>
        /// Gets a list of Candidates that have the rejected flag set at a specific value.
        /// </summary>
        /// <param name="rejected">The flag value to return.</param>
        /// <returns>A list of candidates.</returns>
        private ReadOnlyCandidateCollection GetList(bool rejected)
        {
            CandidateCollection retVal = new CandidateCollection();
            foreach (Candidate c in this)
            {
                if (c.Rejected == rejected) retVal.Add(c);
            }
            return new ReadOnlyCandidateCollection(retVal);
        }

        #endregion

    }


    /// <summary>
    /// Object to represent a read only list of Candidate objects.
    /// </summary>
    /// <remarks>
    /// Author: Keith Jackson
    /// Date: July 2008
    /// Copyright 2008 S-Cool Ltd.
    /// </remarks>
    public class ReadOnlyCandidateCollection : ReadOnlyCollection<Candidate>
    {

        #region | Construction |

        public ReadOnlyCandidateCollection(CandidateCollection sourceList)
            : base(sourceList)
        { }

        #endregion
    
    }

}
